namespace Stee.Cap8.Service
{
    using System.Threading.Tasks;
    using Grpc.Core;
    using Microsoft.Extensions.Logging;

    public class GrpcHealthService : GrpcHealth.GrpcHealthBase
    {
        private readonly ILogger<GrpcHealthService> logger;
        public GrpcHealthService(ILogger<GrpcHealthService> logger) => this.logger = logger;

        public override Task<HealthCheckResponse> Check(HealthCheckRequest request, ServerCallContext context)
        {
            var response = new HealthCheckResponse();
            if(GrpcServiceContext.IsReady())
            {
                if( GrpcServiceContext.IsDegraded())
                {
                   response.Status = HealthCheckResponse.Types.ServingStatus.Degraded;
                }
                else
                {
                    response.Status = HealthCheckResponse.Types.ServingStatus.Serving;
                }
            }
            else
            {
                response.Status = HealthCheckResponse.Types.ServingStatus.NotServing;
            }
            if(this.logger.IsEnabled(LogLevel.Trace))
            {
                this.logger.LogTrace($"Health Check: {response.Status}");
            }
            return Task.FromResult(response); 
        }

      
    }
}
